<!--会议室页-->
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">会议室管理</span>
    <el-button  type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
  <!-- 印会议室搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="会议室名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="room.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select placeholder="请选择" v-model="room.status">
          <el-option v-for="item in statusRoomArr" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="容纳人数">
        <el-input placeholder="请输入" style="width:220px;" v-model="room.capacity"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRoomInfo">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0px 40px 10px;">
      会议室类型
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addRoomType" >新增印章</el-button>
    </div>
    <el-table :data="roomInfoArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="会议室名称" align="center"/>
      <el-table-column prop="capacity" label="容量" align="center"/>
      <el-table-column prop="status" label="状态" align="center"
                       :formatter="statusRoomFormatter"/>
      <el-table-column prop="location" label="位置" align="center"/>
      <el-table-column width="150" label="创建时间" align="center">
        <template #default="scope">
          {{scope.row.createdDate | formatDate}}
        </template>
      </el-table-column>
      <el-table-column width="150" label="更新时间" align="center">
        <template #default="scope">
          {{scope.row.updatedDate | formatDate}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editRoomInfo(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteRoomInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 新增会议室类型弹窗 -->
  <el-dialog  style="width:1000px;padding:40px;" v-model="dialogVisible" :title="dialogTitle"
              :before-close="handleClose">
    <el-form label-width="80px" label-position="top" >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="名称" >
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="1" v-model="saveSealInfoForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="容量">
            <el-select placeholder="请选择" v-model="saveRoomInfoForm.userID">
              <el-option v-for="item in departmentTypeArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="房间状态">
            <el-select placeholder="请选择" v-model="saveRoomInfoForm.status" >
              <el-option v-for="item in statusRoomArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible=false" >取消</el-button>
      <el-button type="primary" @click="savaRoomInfo">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>

import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {dayjs, ElMessage} from "element-plus";

//控制新增弹窗是否显示
const dialogVisible = ref(false);
//定义弹窗
const dialogTitle = ref('新增');
//1.定义数组存放印章表单数据
const roomInfoArr = ref([]);
//定义搜索关键字
const room = ref({
  name: '',
  status: '',
  capacity: ''
});
//定义日期方法
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD');
}
//定义处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭弹窗吗?')){
    saveRoomInfoForm.value = {};
    dialogVisible.value = false;
  }
}
//定义加载方法
const loadRoomInfo =()=>{
  let data = qs.stringify(room.value)
  axios.get(BASE_URL+'/oa1/room/select?'+data).then((response)=>{
    if(response.data.code === 2000){
      roomInfoArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  loadRoomInfo()
})
//定义新增房间类型
const addRoomType = ()=>{
  dialogVisible.value = true;
}
//定义重置搜索
const resetSearch = ()=>{
  room.value = {};
  loadRoomInfo();
}

//对印章状态定义
const statusRoomArr = ref([]);
const statusRoomFormatter = (row,column,cellValue,index)=>{
  if(!statusRoomArr.value){
    return cellValue;
  }
  for(let item of statusRoomArr.value){
    if(item.value === cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

onMounted(()=>{
  loadDictOption(statusRoomArr,'room_status');
})

//定义一个对象保存表单数据
const saveRoomInfoForm =ref({
  roomID:'',
  name:'',
  capacity:'',
  status:'',
  location:'',
  createdDate:'',
  updatedDate:''
})
const savaRoomInfo =()=>{
  dialogTitle.value='新增房间类型';
  let data = qs.stringify(saveRoomInfoForm.value)
  axios.post(BASE_URL+'/oa1/room/save',data).then((response)=>{
    if(response.data.code === 2000){
      ElMessage.success('保存成功!');
      //保存成功后,需要清空表单数据,关掉弹窗
      saveRoomInfoForm.value = {};
      dialogVisible.value = false;
      loadRoomInfo();
    }else{
      //数据库里的code和license设置了唯一约束,如果插入重复的值会操作失败,我们可以设置明确的提示语句
      ElMessage.error(response.data.msg);
    }
  })
}
const editRoomInfo = (id)=>{
  console.log(id)
  dialogVisible.value = true;
  dialogTitle.value='修改房间类型';
  axios.get(BASE_URL+'/oa1/room/select?id='+id).then((response)=>{
    if(response.data.code === 2000){
      saveRoomInfoForm.value = response.data.data[0];
      dialogVisible.value = true;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//删除
const deleteRoomInfo = (id)=>{
  if(confirm('您确认要删除吗?')){
    axios.post(BASE_URL+'/oa1/room/delete/'+id).then((response)=>{
      if(response.data.code === 2000){
        ElMessage.success('删除成功!');
        loadRoomInfo();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}

</script>

<style scoped>

</style>